package com.github.zzay.care.controller;

import com.github.zzay.care.dto.ElderlyDto;
import com.github.zzay.care.entity.Elderly;
import com.github.zzay.care.service.ElderlyService;
import com.github.zzay.common.core.model.ResponseBean;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zzay
 * @className ElderlyController
 * @description Controller class of "Elderly" related API
 * @create 2022/07/07 09:23
 */
@Slf4j
@RestController
@RequestMapping("/elderly")
@CrossOrigin
@Tag(name = "ElderlyController", description = "老人信息操作相关接口")
public class ElderlyController {

    /**
     * Elderly Service.
     */
    @Autowired
    private ElderlyService elderlyService;

    /**
     * Add an elderly into the system.
     *
     * @param elderlyDto Elderly DTO
     * @return Operation Result
     */
    @Operation(summary = "录入老人信息", description = "录入老人信息：系统管理员通过录入表单输入相关信息，执行录入操作",
            security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "录入信息成功"),
            @ApiResponse(responseCode = "400", description = "录入信息失败")
    })
    @PostMapping("/add")
    public ResponseBean<Map<String, Object>> addElderly(
            @Parameter(name = "elderlyDto") @RequestBody ElderlyDto elderlyDto) {
        Map<String, Object> data = new HashMap<>();
        // get related information
        String name = elderlyDto.getName();
        String idCard = elderlyDto.getIdCard();
        String roomNumber = elderlyDto.getRoomNumber();
        String sex = elderlyDto.getSex();
        String phone = elderlyDto.getPhone();
        Date birthday = elderlyDto.getBirthday();
        Date checkInDate = elderlyDto.getCheckInDate();
        Elderly elderly = new Elderly(name, idCard, roomNumber, sex, phone, birthday, checkInDate);
        // save the new entity
        Boolean success = elderlyService.save(elderly);
        data.put("success", success);
        // add a new elderly person successfully
        if (success) {
        log.info("======== AddElderly - Success ========");
            return new ResponseBean<>(HttpStatus.OK.value(), "Successfully add a new elderly person", data);
        }
        // failed to add a new elderly person
        log.info("======== AddElderly - Fail ========");
        return new ResponseBean<>(HttpStatus.BAD_REQUEST.value(), "Failed to add a new elderly person", data);
    }

    /**
     * Delete an elderly person's information.
     *
     * @param elderlyDto Elderly DTO
     * @return Operation Result
     */
    @Operation(summary = "删除老人信息", description = "删除老人信息：系统管理员通过删除表单输入相关信息，执行删除操作",
            security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "删除信息成功"),
            @ApiResponse(responseCode = "400", description = "删除信息失败")
    })
    @PostMapping("/delete")
    public ResponseBean<Map<String, Object>> deleteElderly(
            @Parameter(name = "elderlyDto") @RequestBody ElderlyDto elderlyDto) {
        Map<String, Object> data = new HashMap<>();
        Boolean success = elderlyService.deleteInfo(elderlyDto.getIdCard());
        data.put("success", success);
        // delete an elderly person's information successfully
        if (success) {
            log.info("======== DeleteElderly - Success ========");
            return new ResponseBean<>(HttpStatus.OK.value(), "Successfully delete the information", data);
        }
        // failed to delete an elderly person's information
        log.info("======== DeleteElderly - Fail ========");
        return new ResponseBean<>(HttpStatus.BAD_REQUEST.value(), "Failed to delete the information", data);
    }

    /**
     * Update an elderly person's information.
     *
     * @param elderlyDto Elderly DTO
     * @return Operation Result
     */
    @Operation(summary = "更新老人信息", description = "更新老人信息：系统管理员通过更新表单输入相关信息，执行更新操作",
            security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "更新信息成功"),
            @ApiResponse(responseCode = "400", description = "更新信息失败")
    })
    @PostMapping("/update")
    public ResponseBean<Map<String, Object>> updateElderly(
            @Parameter(name = "elderlyDto") @RequestBody ElderlyDto elderlyDto) {
        Map<String, Object> data = new HashMap<>();
        // update the entity
        Boolean success = elderlyService.updateInfo(elderlyDto);
        data.put("success", success);
        // update an elderly person's information successfully
        if (success) {
            log.info("======== UpdateElderly - Success ========");
            return new ResponseBean<>(HttpStatus.OK.value(), "Successfully update the information", data);
        }
        // failed to update an elderly person's information
        log.info("======== UpdateElderly - Fail ========");
        return new ResponseBean<>(HttpStatus.BAD_REQUEST.value(), "Failed to update the information", data);
    }

    /**
     * Query all information.
     *
     * @return Operation Result
     */
    @Operation(summary = "查询所有老人信息", description = "查询所有老人信息：系统查询所有老人信息并返回",
            security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询信息成功"),
            @ApiResponse(responseCode = "400", description = "查询信息失败")
    })
    @GetMapping("/getAll")
    public ResponseBean<Map<String, Object>> getAllElderly() {
        Map<String, Object> data = new HashMap<>();
        List<ElderlyDto> elderlyList = elderlyService.getAll();
        if (elderlyList == null) {
            // failed to get information
            data.put("success", false);
            log.info("======== GetAllElderly - Fail ========");
            return new ResponseBean<>(HttpStatus.BAD_REQUEST.value(), "Failed to get the information", data);
        } else {
            // get information successfully
            data.put("success", true);
            data.put("info", elderlyList);
            log.info("======== GetAllElderly - Success ========");
            return new ResponseBean<>(HttpStatus.OK.value(), "Successfully get the information", data);
        }
    }

    /**
     * Query an elderly person's information according to the Id card number.
     *
     * @param elderlyDto Elderly DTO
     * @return Operation Result
     */
    @Operation(summary = "查询老人信息", description = "查询老人信息：系统管理员根据老人身份证号，执行查询操作",
            security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询信息成功"),
            @ApiResponse(responseCode = "400", description = "查询信息失败")
    })
    @GetMapping("/getById")
    public ResponseBean<Map<String, Object>> getElderlyById(
            @Parameter(name = "elderlyDto") @RequestBody ElderlyDto elderlyDto) {
        Map<String, Object> data = new HashMap<>();
        ElderlyDto elderly = elderlyService.getInfoById(elderlyDto.getIdCard());
        if (elderly == null) {
            // failed to get an elderly person's information
            data.put("success", false);
            log.info("======== GetElderlyById - Success ========");
            return new ResponseBean<>(HttpStatus.BAD_REQUEST.value(), "Failed to get the information", data);
        } else {
            // get an elderly person's information successfully
            data.put("success", true);
            data.put("info", elderly);
            log.info("======== GetElderlyById - Fail ========");
            return new ResponseBean<>(HttpStatus.OK.value(), "Successfully get the information", data);
        }
    }

    /**
     * Query an elderly person's information according to the name.
     *
     * @param elderlyDto Elderly DTO
     * @return Operation Result
     */
    @Operation(summary = "查询老人信息", description = "查询老人信息：系统管理员根据老人姓名，执行查询操作",
            security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询信息成功"),
            @ApiResponse(responseCode = "400", description = "查询信息失败")
    })
    @GetMapping("/getByName")
    public ResponseBean<Map<String, Object>> getElderlyByName(
            @Parameter(name = "elderlyDto") @RequestBody ElderlyDto elderlyDto) {
        Map<String, Object> data = new HashMap<>();
        ElderlyDto elderly = elderlyService.getInfoByName(elderlyDto.getName());
        if (elderly == null) {
            // failed to get an elderly person's information
            data.put("success", false);
            log.info("======== GetElderlyByName - Success ========");
            return new ResponseBean<>(HttpStatus.BAD_REQUEST.value(), "Failed to get the information", data);
        } else {
            // get an elderly person's information successfully
            data.put("success", true);
            log.info("======== GetElderlyByName - Fail ========");
            return new ResponseBean<>(HttpStatus.OK.value(), "Successfully get the information", data);
        }
    }

}

